package com.mallbook.model.utils;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @Author 宋明松
 * @Version V1.0.0
 * @Date 2022-07-07
 */
public class MailUtil {
    public static void sendMail(String email) {
        try {
            final Properties props = new Properties();
            props.put("mail.user", "2929511417@qq.com");   //发件方的邮箱
            props.put("mail.password", "cnsdauvpdinbdgea"); // 授权码（密码）
            props.put("mail.smtp.auth", "true");
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.qq.com");
            props.put("mail.smtp.port", "25");

            Session mailSession = Session.getDefaultInstance(props);

            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress("2929511417@qq.com")); // 发件人
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); // 收件人
            msg.setSubject("用户注册激活邮件-蜗牛学院"); // 主题
            msg.setContent("<h1>此邮件为蜗牛学院电商平台激活邮件！请点击下面链接完成激活操作！</h1><br />" + "此邮件30分钟有效!"
                            + "<h4><a href='http://qzc49x.natappfree.cc/user?opr=active&email=" + email + "'>http://qzc49x.natappfree.cc/user?opr=active&email=" + email + "</a></h4>",
                    "text/html;charset=UTF-8");
            msg.saveChanges();

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"), props.getProperty("mail.user"),
                    props.getProperty("mail.password"));
            transport.sendMessage(msg, msg.getAllRecipients());
            transport.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
